package es.ats.codington.spring.controllers;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.PlaceService;



/**
 * The Class EventController.
 */
@Controller
public class EventController {

	
	/**
	 * Show event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/show_event.htm")
	public ModelAndView showEventController(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		EventService es = new EventService();
		Object iD = request.getSession().getAttribute("iDVisitor");
		String msg = request.getParameter("msg");
		int idevent = Integer.parseInt(request.getParameter("idevent"));
	    int id= (Integer) iD;
	    
	    try{
	    	   
			ArrayList<Integer> myidevents = es.viewMyEvent(id);
			int size = myidevents.size();
			boolean signed=false;
			int iter=0;
			while (iter<size && signed==false){
				if(idevent==myidevents.get(iter)){
					signed=true;
				}else{
					signed=false;
				}
				iter++;
			}
			String myString = null; 
			myString = String.valueOf(signed);
			modelAndView.addObject("signed",myString);
			modelAndView.addObject("msg", msg);
		
			Event evento = es.viewEvent(idevent);
			modelAndView.addObject("evento", evento);
			modelAndView.setViewName("viewevent");
						
			
	    }catch(Exception ex){
	    	ex.getMessage();
		}
		
		return modelAndView;		
	}
	
	/**
	 * North event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/north_event.htm")
	public ModelAndView northEventController(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		EventService es = new EventService();
		PlaceService ps = new PlaceService();
		Event event = new Event();
		ArrayList<Integer> idsevent = new ArrayList<Integer>();
		ArrayList<Integer> idsplace = new ArrayList<Integer>();
		ArrayList<Event> allevents = new ArrayList<Event>();
		int[] iter = new int[4];
		int[] size = new int[4];
		
		try{
			String type1 = (String) request.getParameter("type1");
			idsplace= ps.getIdsPlace(type1);
			size[0]= idsplace.size();
			
			for (iter[0]=0; iter[0]<size[0]; iter[0]++){
				int id= idsplace.get(iter[0]);
			
				idsevent = es.getIdsEvent(id);
				
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					
					allevents.add(event);
				}
			}
				
			String type2 = (String) request.getParameter("type2");
			idsplace= ps.getIdsPlace(type2);
			size[1]= idsplace.size();
			
			for (iter[1]=0; iter[1]<size[1]; iter[1]++){
				int id= idsplace.get(iter[1]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
					
			String type3 = (String) request.getParameter("type3");
			idsplace= ps.getIdsPlace(type3);
			size[2]= idsplace.size();
			
			for (iter[2]=0; iter[2]<size[2]; iter[2]++){
				int id= idsplace.get(iter[2]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
						
			String type4 = (String) request.getParameter("type4");
			idsplace= ps.getIdsPlace(type4);
			size[3]= idsplace.size();
			
			for (iter[3]=0; iter[3]<size[3]; iter[3]++){
				int id= idsplace.get(iter[3]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
					
			int siz= allevents.size();
			
			modelAndView.addObject("size", siz);
			modelAndView.addObject("events", allevents);
			modelAndView.setViewName("NorthEvents");
			
		}catch(Exception ex){
			ex.getMessage();
			modelAndView.setViewName("mainpage2");
			
			
		}
		return modelAndView;
	}
	
	/**
	 * South event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/south_event.htm")
	public ModelAndView southEventController(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		EventService es = new EventService();
		PlaceService ps = new PlaceService();
		Event event = new Event();
		ArrayList<Integer> idsevent = new ArrayList<Integer>();
		ArrayList<Integer> idsplace = new ArrayList<Integer>();
		ArrayList<Event> allevents = new ArrayList<Event>();
		int[] iter = new int[4];
		int[] size = new int[4];
		
		try{
			String type1 = (String) request.getParameter("type1");
			
			idsplace= ps.getIdsPlace(type1);
			size[0]= idsplace.size();
			
			for (iter[0]=0; iter[0]<size[0]; iter[0]++){
				int id= idsplace.get(iter[0]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);
				}
			}
			int sizz = allevents.size();
	
			
			String type2 = (String) request.getParameter("type2");
			
			idsplace= ps.getIdsPlace(type2);
			size[1]= idsplace.size();
			
			for (iter[1]=0; iter[1]<size[1]; iter[1]++){
				int id= idsplace.get(iter[1]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
			int sizz2 = allevents.size();

			
			String type3 = (String) request.getParameter("type3");
			idsplace= ps.getIdsPlace(type3);
			size[2]= idsplace.size();
			
			for (iter[2]=0; iter[2]<size[2]; iter[2]++){
				int id= idsplace.get(iter[2]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
			int sizz3 = allevents.size();

			
			String type4 = (String) request.getParameter("type4");
			idsplace= ps.getIdsPlace(type4);
			size[3]= idsplace.size();
			
			for (iter[3]=0; iter[3]<size[3]; iter[3]++){
				int id= idsplace.get(iter[3]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
			int sizz4 = allevents.size();
				

			int siz= allevents.size();
			modelAndView.addObject("size", siz);
			modelAndView.addObject("events", allevents);
			modelAndView.setViewName("SouthEvents");
			
			
		}catch(Exception ex){
			ex.getMessage();
			modelAndView.setViewName("mainpage2");
			
		}
		return modelAndView;
		
	}
	

	/**
	 * Show my event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/showmy_event.htm")
	public ModelAndView showMyEventController(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		Object iD = request.getSession().getAttribute("iDVisitor");
	    int id= (Integer) iD;
		EventService es = new EventService();
		ArrayList<Event> myevents = new ArrayList<Event>();
		try{
			
		ArrayList<Integer> myidevents = es.viewMyEvent(id);
		int size = myidevents.size();
		modelAndView.addObject("size", size);
		
		for (int iter=0; iter<size; iter++){
			Event event = es.viewEvent(myidevents.get(iter));
			myevents.add(event);			
		}
	
		modelAndView.addObject("evento", myevents);
		modelAndView.setViewName("viewmyevents");
		

		}catch(Exception ex){
			ex.getMessage();
			
		}		
		return modelAndView;
	}
	
	/**
	 * Pre createvent.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/pre_createvent.htm")
	public String preCreatevent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "neweventregistrationadd";
	}
	
	/**
	 * Post createvent.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@RequestMapping(value= "/post_createvent.htm")
	public String postCreatevent(HttpServletRequest request, HttpServletResponse response) throws IOException {

		try{
			
		String namevent=request.getParameter("namevent");
		int price=Integer.parseInt(request.getParameter("price"));
		String description=request.getParameter("descriptions");
		int idplace=Integer.parseInt(request.getParameter("idplace"));
		int seats=Integer.parseInt(request.getParameter("seats"));
		
		Event event=new Event();
		
		PlaceService ps = new PlaceService();
		Place place = ps.viewPlace(idplace);

        event.setNamevent(namevent);  
        event.setPrice(price); 
        event.setDescription(description); 
        event.setSeats(seats);
        event.setPlace(place);
		          
        EventService es=new EventService();
        
      	boolean tt =es.createEvent(event);
 		    if (tt==true){	
 		    	return "mainpage2"; 
 		    
 		    }else{  
 		      return "neweventregistrationadd";  
 		    }
 	    }catch(Exception ex){
 			ex.getMessage();
 			return "mainpage2";
 		}     

	 
		          
		}
	}

	
